package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody CartItem cartItem){

        this.cartService.saveCartItem(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询当前用户的所有的购物车数据
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<CartItem>> listCarts(){
        return ResponseEntity.ok(this.cartService.listCarts());
    }

    /**
     * 修改某个sku的数量
     * @param skuId
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> modifyCartNum(
            @RequestParam("id")Long skuId,
            @RequestParam("num")Integer num){

        this.cartService.modifyCartNum(skuId,num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId")Long skuId){
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/list")
    public ResponseEntity<Void> mergeCarts(@RequestBody List<CartItem> cartItems){
        this.cartService.mergeCarts(cartItems);
        return ResponseEntity.ok().build();
    }

}
